package com.kb.erp.service.impl;

import cn.hutool.http.HttpUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.kb.erp.domain.DBPCBECNNOTICEEntity;
import com.kb.erp.mapper.sqlServer.DBPCBECNNOTICEMapper;
import com.kb.erp.service.DBPCBECNNOTICEService;
import com.kb.erp.util.MessageUtil;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
@DS("slave-1")
@Slf4j
public class DBPCBECNNOTICEServiceImpl implements DBPCBECNNOTICEService {

    @Autowired
    private DBPCBECNNOTICEMapper DBPCBECNNOTICEMapper;

    @Override
    public void spSendQwMessage() {



        try {

            List<DBPCBECNNOTICEEntity> DBPCBECNNOTICEList = DBPCBECNNOTICEMapper.selectTopList(50);
            if (CollectionUtils.isEmpty(DBPCBECNNOTICEList)) {
                return;
            } else {
                for (DBPCBECNNOTICEEntity DBPCBECNNOTICE : DBPCBECNNOTICEList) {
                    long RKEY = DBPCBECNNOTICE.getRkey();
                    String partnumber=DBPCBECNNOTICE.getPartnumber();
                    String ecnno=DBPCBECNNOTICE.getEcnno();
                    String job_number=DBPCBECNNOTICE.getJob_number();
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    LocalDateTime currentDateTime = LocalDateTime.now();
                    String formattedNow = currentDateTime.format(formatter);
                    String sendcontent="有新的编号"+ecnno+"关联档案号"+partnumber+'('+formattedNow+')';

                    String keyUrl = "http://192.168.10.22:8153/erphandler.ashx?cmd=sendqwmessage";
                    HashMap<String, Object> paramMap = new HashMap<>();
                    paramMap.put("job_number", job_number);
                    paramMap.put("sendcontent", sendcontent);

                    log.info(keyUrl);
                    XxlJobHelper.log(keyUrl);

                    String reponse = HttpUtil.post(keyUrl, paramMap);
                    if(reponse.equals( "{\"code\":0,\"msg\":\"OK\"}") )
                    {

                        DBPCBECNNOTICE.setSyndate(new Date());
                        DBPCBECNNOTICE.setSynstate(0);
                        DBPCBECNNOTICE.setSynmsg(reponse);
                        DBPCBECNNOTICEMapper.updateDBPCBECNNOTICEEntity(DBPCBECNNOTICE);
                        int count= DBPCBECNNOTICEMapper.updateDBPCBECNNOTICEEntityERROR(DBPCBECNNOTICE);
                        if (count>0){
                            log.info("批量更新成功！");
                        }else {
                            log.info("批量更新失败！");
                        }
                        log.info("消息通知成功！");
                    } else {
                        DBPCBECNNOTICE.setSyndate(new Date());
                        DBPCBECNNOTICE.setSynstate(0);
                        DBPCBECNNOTICE.setSynmsg(reponse);
                        DBPCBECNNOTICEMapper.updateDBPCBECNNOTICEEntity(DBPCBECNNOTICE);

                        int count= DBPCBECNNOTICEMapper.updateDBPCBECNNOTICEEntityERROR(DBPCBECNNOTICE);
                        if (count>0){
                            log.info("批量更新成功！");
                        }else {
                            log.info("批量更新失败！");
                        }

                        log.info("消息通知失败！");
                    }
                }
            }

        } catch (Exception e) {
            MessageUtil.sentMessageToAssistant("18597", e.getMessage());
            log.info(e.getMessage());
            XxlJobHelper.log(e.getMessage());
            throw new RuntimeException(e);

        }

    }
}
















